<?php

namespace App\Services\impl;

use App\Model\CommonSpiderItem;
use App\Services\spider\CommonSpider;
use App\Services\spider\SpiderItemRepo;
use App\Services\spider\TypeValidator;
use App\Services\UtilService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

/**
 * 全国医院大全
 */
class QgyydqService
{
    private function getSpider(): CommonSpider
    {
        $typeValidator = new TypeValidator([new QgyydqTypeParser("qgyydq",
            "/^(https:\/\/shuju.qgyyzs.net)?(http:\/\/shuju.qgyyzs.net)?(shuju.qgyyzs.net)?\/qgyydq\/details[0-9a-zA-Z]+\.html$/si"
        )]);
        $repo = new SpiderItemRepo();
        $spider = new  CommonSpider($this->getUrls(), "qgyydq-0630", $typeValidator, new QgyydqUrlParser($repo));
        return $spider;
    }

    public function addUrls()
    {
        $urls = $this->getUrls();
        $this->getSpider()->addItems($urls);
        dd("x");
    }

    public function start()
    {

        $this->getSpider()->start();
    }

    public function autoAddQgyydqItemByName()
    {
        $spider = $this->getSpider();
        $url = "";
        while (true) {
            $limit = 100;
            $items = CommonSpiderItem::query()
                ->where("task_no", '=', $spider->getTaskNo())
                ->where("is_target", '=', 1)
                ->where("parse_status", '=', 1)
                ->where("url", '>', $url)
                ->limit($limit)
                ->get([DB::raw('data->"$.name" as name'), 'url']);
            foreach ($items as $item) {
                $tokens = UtilService::getInstance()->tokenizer($item->name, 2);
                $urls = [];
                foreach ($tokens as $token) {
                    $iUrl = "https://shuju.qgyyzs.net/qgyydq/?key=$token&terms=address";;
                    $urls[] = $iUrl;
                    UtilService::getInstance()->print("add url:$iUrl");
                }
                $spider->addItems($urls);
            }
            if ($items->count() < $limit) {
                break;
            }
            $url = $items[$limit - 1]->url;
        }

    }


    public function getUrls(): array
    {
//        $urls = ["https://shuju.qgyyzs.net/qgyydq/"];
        $keys = [
            "公司", "有限", "北京", "上海", "深圳", "济南", "生物", "科技",
            "安徽", "澳门", "重庆", "福建", "甘肃", "广东", "广西", "贵州", "海南", "河北", "河南", "黑龙江", "湖北",
            "湖南", "吉林", "江苏", "江西", "辽宁", "内蒙古", "宁夏", "青海", "山东", "山西", "陕西", "上海", "四川", "台湾",
            "天津", "西藏", "香港", "新疆", "云南", "浙江", "公司", "医院", "省", "市区", '县', "政府", "乡", "协和", "人民", "医院",
            "附属"
            , "专科", "双桥", "社区", "卫生", "民航", "总", "学科", "科学", "中心"
//            ,"","","",""
//            ,"","","","","","","","","","","","",""
        ];
        foreach ($keys as $key) {
//            $urls[] = "https://shuju.qgyyzs.net/qgyydq/?key=$key&terms=hospitalname";
            $urls[] = "https://shuju.qgyyzs.net/qgyydq/?key=$key&terms=address";
            for ($i = 1; $i < mb_strlen($key); $i++) {
                $char = Str::substr($key, $i, 1);
//                $urls[] = "https://shuju.qgyyzs.net/qgyydq/?key=$char&terms=hospitalname";
                $urls[] = "https://shuju.qgyyzs.net/qgyydq/?key=$char&terms=address";
            }
        }
        return $urls;
    }

    public function handleUrl($url)
    {
        $this->getSpider()->handleUrl($url);
    }


}